﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace UFODefense
{
    class Explosion : DrawableGameComponent
    {
        SpriteBatch m_spriteBatch;
        ContentManager m_contentManager;
        Texture2D[] m_explosionFrames;
        Vector2 m_position;
        float m_creationTime;
        bool m_timeSet;
        SoundEffect m_explosionS;
        float m_radius;

        public Explosion(Game game, SpriteBatch SB, ContentManager CM, Vector2 position)
            : base(game)
        {
            m_spriteBatch = SB;
            m_contentManager = CM;
            m_position = position;

            Game.Components.Add(this);
        }

        public override void Initialize()
        {
            m_explosionFrames = new Texture2D[Definitions.EXPLOSION_FRAMES];
            m_timeSet = false;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            m_explosionFrames[0] = m_contentManager.Load<Texture2D>("GFX/explosionF1");
            m_explosionFrames[1] = m_contentManager.Load<Texture2D>("GFX/explosionF2");
            m_explosionFrames[2] = m_contentManager.Load<Texture2D>("GFX/explosionF3");
            m_explosionFrames[3] = m_contentManager.Load<Texture2D>("GFX/explosionF4");
            m_explosionFrames[4] = m_contentManager.Load<Texture2D>("GFX/explosionF5");
            m_explosionFrames[5] = m_contentManager.Load<Texture2D>("GFX/explosionF6");
            m_explosionFrames[6] = m_contentManager.Load<Texture2D>("GFX/explosionF7");
            m_explosionFrames[7] = m_contentManager.Load<Texture2D>("GFX/explosionF8");
            m_explosionFrames[8] = m_contentManager.Load<Texture2D>("GFX/explosionF9");
            m_explosionFrames[9] = m_contentManager.Load<Texture2D>("GFX/explosionF10");

            m_explosionS = m_contentManager.Load<SoundEffect>("SFX/explosion");

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            float currentTime = (float)gameTime.TotalGameTime.TotalMilliseconds;
            
            //set creationtime if not set
            if (!m_timeSet)
            {
                m_creationTime = currentTime;
            }
            float lifeTime = currentTime - m_creationTime;
            int index = (int)Math.Floor(lifeTime / 100);
            //make the radius of the explosion half the width of the explosion frame
            m_radius = (m_explosionFrames[index].Width / 2);
            
        
            


            base.Update(gameTime);
        }
    }
}
